@echo off
setlocal

echo.

echo Setting up the ACS Getting Started sample
echo.

SET /P acsolution=Please enter your Service Namespace: 
SET /P acmgmtkey=Please enter your management key: 
echo.

echo Configuring ACS
echo.

SET toolpath=%~dp0..\..\..\..\Tools\

"%toolpath%acm.exe" create tokenpolicy -name:infocorp -autogeneratekey -service:%acsolution% -mgmtkey:%acmgmtkey% -simpleout > tokenpolicy.tmp
SET /p tokenpolicyid=<tokenpolicy.tmp

SET policyCreated=%tokenpolicyid:~0,3%
if "%policyCreated%"=="tp_" ( 
	    echo Token policy created 
	) else ( 
	    goto :error
	)

echo.
"%toolpath%acm.exe" create scope -name:infocorp -appliesto:http://localhost/SalesDashboard/ -tokenpolicyid:%tokenpolicyid% -service:%acsolution% -mgmtkey:%acmgmtkey% -simpleout > scope.tmp
SET /p scopeid=<scope.tmp

SET scopeCreated=%scopeid:~0,4%
if "%scopeCreated%"=="scp_" ( 
	    echo Scope created 
	) else ( 
	    goto :error
	)

echo.
"%toolpath%acm.exe" create issuer -name:infocorp -issuername:infocorp -autogeneratekey -service:%acsolution% -mgmtkey:%acmgmtkey% -simpleout > issuer.tmp
SET /p issuerid=<issuer.tmp

SET issuerCreated=%issuerid:~0,4%
if "%issuerCreated%"=="iss_" ( 
	    echo Issuer created 
	) else ( 
	    goto :error
	)

echo.
"%toolpath%acm.exe" create rule -name:infocorp -scopeid:%scopeid% -inclaimissuerid:%issuerid% -inclaimtype:Issuer -inclaimvalue:gettingstarted -outclaimtype:action -outclaimvalue:reverse -service:%acsolution% -mgmtkey:%acmgmtkey% -simpleout > rule.tmp
SET /p ruleid=<rule.tmp
echo Rule created
echo.

"%toolpath%acm.exe" get issuer -id:%issuerid% -service:%acsolution% -mgmtkey:%acmgmtkey% > key.tmp
findstr /C:=<key.tmp > keyvalue.tmp
SET /P keyvalue=<keyvalue.tmp
echo Client infocorp Issuer %keyvalue%

"%toolpath%acm.exe" get tokenpolicy -id:%tokenpolicyid% -service:%acsolution% -mgmtkey:%acmgmtkey% > tp.tmp
findstr /C:=<tp.tmp > tpkey.tmp
SET /P tpkey=<tpkey.tmp
echo Service Token Policy %tpkey%

echo.
echo Creating Rules for CreateOrder ViewProduct ViewRegion ViewOrder and UpdateOrder
echo 
"%toolpath%acm.exe" create rule -name:CreateOrder -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:CreateOrder -outclaimvalue:true -simpleout > rule.tmp

"%toolpath%acm.exe" create rule -name:ViewProduct -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:ViewProduct -outclaimvalue:true -simpleout > ViewProduct.tmp

"%toolpath%acm.exe" create rule -name:ViewRegion -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:ViewRegion -outclaimvalue:true -simpleout > ViewRegion.tmp

"%toolpath%acm.exe" create rule -name:ViewSalesData -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:ViewSalesData -outclaimvalue:true -simpleout > ViewSalesData.tmp

"%toolpath%acm.exe" create rule -name:ViewOrder -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:ViewOrder -outclaimvalue:true -simpleout > ViewOrder.tmp

"%toolpath%acm.exe" create rule -name:UpdateOrder -scopeid:scp_a0d4c75c7c190cb2121e6a8592cbe82b6bcb1dfe -inclaimissuerid:iss_605fe6e7ac63e819056b94102261ddc9fa8b56eb -inclaimtype:Issuer -inclaimvalue:infocorp -outclaimtype:UpdateOrder -outclaimvalue:true -simpleout > UpdateOrder.tmp

echo Done.

erase *.tmp

goto endscript

:error

echo.
echo Sample setup failed
goto :endscript

:endscript